import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/view/component/MessagePaneBackgroundView.dart';
import 'package:im_app/im/util/ImConstant.dart';
import 'package:im_app/im/util/UiUtil.dart';

import '../../../../pojo/ImPojo.dart';
import '../../../../util/FileUtil.dart';
import '../../../../util/MessageUtil.dart';
import '../../../bloc/MessageListCubit.dart';
import '../../../bloc/MessageSendCubit.dart';
import '../../../ext/MessageSelectionControls.dart';
import '../../../sub/file/FileViewPage.dart';
import '../UiUtil.dart';
import 'MessagePane.dart';

class FileMessagePane extends StatelessWidget {
  final WrapImMessage wrapMessage;

  const FileMessagePane({required this.wrapMessage, super.key});

  @override
  Widget build(BuildContext context) {
    var messageSendCubit = context.read<MessageSendCubit>();
    var messageListCubit = context.read<MessageListCubit>();
    wrapMessage.message.initMessageExt();
    var iconPath = FileUtil.getFileImagePath(FileUtil.getSuffix(wrapMessage.message.messageExt?.originalName??''));
    var filePath = MessageUtil.getFilePath2(wrapMessage.message);
    var isExisted = FileUtil.fileExisted(filePath);
    if (!isExisted) {
      return buildSvgView('assets/svg/error_file.svg', 80, 60, Colors.black54);
    }
    return MessagePaneBackgroundView(
        message: wrapMessage.message,
        child: SelectableText.rich(TextSpan(
          children: [
            WidgetSpan(child: GestureDetector(
              onTap: () {
                openFile(context, wrapMessage.message);
              },
              child: buildFilePane(iconPath),
            ))
          ],
        ),selectionControls: MessageSelectionControls(message: wrapMessage.message, messageSendCubit: messageSendCubit, messageListCubit: messageListCubit)
        )
    );
  }

  Widget buildFilePane(String filePath) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.end,
      mainAxisSize: MainAxisSize.min,
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            ConstrainedBox(
              constraints: const BoxConstraints(maxWidth: 180),
              child: Text(
                wrapMessage.message.messageExt?.originalName ?? '',
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
                style: const TextStyle(fontSize: ImConstant.IM_DEFAULT_SIZE),
              ),
            ),
            const SizedBox(
              height: 5,
            ),
            Text(
              FileUtil.formatFileSize(wrapMessage.message.messageExt?.size??0),
              style: const TextStyle(fontSize: ImConstant.IM_SMALL_SIZE, color: Colors.grey),
            ),
          ],
        ),
        const SizedBox(
          width: 5,
        ),
        Image(
          image: AssetImage(filePath),
          width: 40,
          height: 50,
        )
      ],
    );
  }
}


class FileMessageRefPane extends StatelessWidget {
  final ImMessage message;
  final MessageRefParam param;

  const FileMessageRefPane({required this.message, required this.param, super.key});

  @override
  Widget build(BuildContext context) {
    message.initMessageExt();
    var filePath = FileUtil.getFileImagePath(FileUtil.getSuffix(message.messageExt?.originalName??''));
    return GestureDetector(
      onTap: () {
        openFile(context, message);
      },
      child: buildFilePane(filePath),
    );
  }

  Widget buildFilePane(String filePath) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.end,
      mainAxisSize: MainAxisSize.min,
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            ConstrainedBox(
              constraints: BoxConstraints(maxWidth: param.refMaxLength-30),
              child: Text(
                message.messageExt?.originalName ?? '',
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
                style: const TextStyle(fontSize: ImConstant.IM_DEFAULT_SIZE),
              ),
            ),
            const SizedBox(
              height: 5,
            ),
            Text(
              FileUtil.formatFileSize(message.messageExt?.size??0),
              style: const TextStyle(fontSize: ImConstant.IM_SMALL_SIZE, color: Colors.grey),
            ),
          ],
        ),
        const SizedBox(
          width: 5,
        ),
        Image(
          image: AssetImage(filePath),
          width: 40,
          height: 50,
        )
      ],
    );
  }
}

void openFile(BuildContext context, ImMessage message) {
  if (message.fileStatus != ImMessageFileStatus.downloaded && message.currentUserId != message.fromUserId) {
    return;
  }
  if (!FileUtil.canShow(message.content)) {
    UiUtil.showSnackBar(context, "当前不支持此格式的文件预览");
    return;
  }
  Navigator.of(context).push(MaterialPageRoute(builder: (context){
    return FileViewPage(message: message);
  }));
}
